package com.dkd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkd.dao.ChannelDao;
import com.dkd.entity.ChannelEntity;
import com.dkd.entity.SkuEntity;
import com.dkd.entity.dto.ChannelCodeDto;
import com.dkd.entity.dto.ChannelConfigDto;
import com.dkd.entity.vo.ChannelVo;
import com.dkd.service.ChannelService;
import com.dkd.service.SkuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class ChannelServiceImpl extends ServiceImpl<ChannelDao, ChannelEntity> implements ChannelService {
    private final SkuService skuService;

    /**
     * 获取售货机货道详情
     *
     * @param innerCode
     * @return
     */
    @Override
    public List<ChannelVo> getDetails(String innerCode) {
        List<ChannelVo> channelVos = new ArrayList<>(20);
        //获取
        List<ChannelEntity> channelEntityList = this.lambdaQuery().eq(ChannelEntity::getInnerCode, innerCode).list();
        if (channelEntityList == null) {
            log.info("没有该售货机货道的信息");
            return channelVos;
        }
        //获取商品信息

        for (ChannelEntity channelEntity : channelEntityList) {
            SkuEntity skuEntity = skuService.getById(channelEntity.getSkuId());
            channelEntity.setSku(skuEntity);
            ChannelVo channelVo = new ChannelVo();
            BeanUtils.copyProperties(channelEntity, channelVo);
            //设置商品信息
            channelVo.setSkuName(skuEntity.getSkuName());
            channelVo.setSkuImage(skuEntity.getSkuImage());
            channelVos.add(channelVo);
        }
        return channelVos;
    }

    /**
     * 货道配置
     *
     * @param channelConfigDto
     * @return
     */
    @Override
    @Transactional
    public boolean channelConfig(ChannelConfigDto channelConfigDto) {
        List<ChannelEntity> channelEntityList = this.lambdaQuery().eq(ChannelEntity::getInnerCode, channelConfigDto.getInnerCode()).list();
        List<ChannelCodeDto> channelList = channelConfigDto.getChannelList();
        //设置查询列表
        List<String> ChannelCodeList = channelList.stream().map(c -> c.getChannelCode()).collect(Collectors.toList());
        //设置商品--列表对应信息
        Map<String, String> map = channelList.stream()
                .collect(Collectors.toMap(channelCodeDto -> channelCodeDto.getChannelCode(), channelCodeDto -> channelCodeDto.getSkuId()));
        //修改信息
        List<ChannelEntity> list = this.lambdaQuery()
                .eq(ChannelEntity::getInnerCode, channelConfigDto.getInnerCode())
                .in(ChannelEntity::getChannelCode, ChannelCodeList)
                .list();
        //遍历列表，修改ChannelEntity信息
        for (ChannelEntity channelEntity : list) {
            String skuId = map.get(channelEntity.getChannelCode());
            channelEntity.setSkuId(Long.parseLong(skuId));
        }
        boolean b = updateBatchById(list);
        log.info("b==============={}", b);
        return b;
    }
}
